package com.rc.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.activity.provider.Contract;
import com.rc.adapter.TripItemAdapter;
import com.rc.custom.MyListView;
import com.rc.db.DatabaseSupport;
import com.telpoo.frame.ui.BaseActivity;

public class TripList extends BaseActivity implements OnItemClickListener {

	// call Intent
	public static void callIntent(Activity ct) {
		Intent it = new Intent(ct, TripList.class);
		ct.startActivityForResult(it, 2);
	}

	public static void callIntentFromReport(Context ct, long isReport) {
		Intent it = new Intent(ct, TripList.class);
		it.putExtra("IS_REPORT", isReport);
		ct.startActivity(it);
	}

	// call Intent categorize
	public static void callIntentCategorize(Activity ct,
			ArrayList<String> arrReceipID) {
		Intent it = new Intent(ct, TripList.class);
		Bundle value = new Bundle();
		value.putStringArrayList("ITEM_ID", arrReceipID);
		it.putExtras(value);
		ct.startActivityForResult(it, 2);
	}

	private MyListView lvTripItem = null;
	private ArrayList<tbl_Trip> arrTrip = null;
	private TripItemAdapter adpTripItem = null;
	private ArrayList<String> arrItems;
	private long mReportID = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.categorize_list);

		((TextView) findViewById(R.id.txtTitle))
				.setText(getString(R.string.trip_list));
		lvTripItem = (MyListView) findViewById(R.id.lvCategorize);

		// get all trip non report
		arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(this.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				tbl_TripDao.Properties.Report_ID.columnName + " = '0' and "
						+ tbl_TripDao.Properties.IsCreate.columnName
						+ " != '1'", null, null));
		arrItems = new ArrayList<String>();
		adpTripItem = new TripItemAdapter(this, arrTrip);
		lvTripItem.setAdapter(adpTripItem);
		lvTripItem.setOnItemClickListener(this);
		if (getIntent().hasExtra("ITEM_ID")) {
			arrItems = getIntent().getStringArrayListExtra("ITEM_ID");
		}
		if (getIntent().hasExtra("IS_REPORT")) {
			mReportID = getIntent().getLongExtra("IS_REPORT", 0);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (arrItems.size() > 0) {
			CategorizeItems.callIntentCategorize(TripList.this, arrItems,
					arrTrip.get(position).getTripID());
		} else if (mReportID != 0) {
			DatabaseSupport.addTripToReport(mReportID, arrTrip.get(position)
					.getTripID(), this);
			finish();
		} else {
			Intent i = getIntent();
			i.putExtra("TRIP_ID", arrTrip.get(position).getTripID());
			setResult(2, i);
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}

}
